package net.sharpf2.api.util.web;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class GELookup {
    // Item information pulled from GrandExchange

    private final String url = "http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=";

    /**
     * Looksup an specified item.
     * @param   id  Item ID.
     * @return  GEItem.
     */
    public GEItem lookup(int id) {
        int price = 0;
        String name = "";
        String input = "";
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(new URL(url + id).openStream()));
            while ((input = in.readLine()) != null && !Thread.currentThread().isInterrupted()) {
                if (input.contains("<title>RuneScape - Grand Exchange</title>")) {
                    return null;
                }
                if (input.contains(" - Grand Exchange - RuneScape</title>")) {
                    name = input.replace(" - Grand Exchange - RuneScape</title>", "").replace("<title>", "");
                }
                if (input.contains("Current guide price:")) {
                    price = parse(input.replace("<b>Current guide price:</b> ", ""));
                }
            }
            in.close();
            return new GEItem(name, price);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private int parse(String str) {
        String s = "0";
        if (str.contains("k")) {
            s = str.replace("k", "");
            if (s.contains(".")) {
                s = s.replace(".", "");
                return (Integer.parseInt(s) * 100);
            } else {
                return (Integer.parseInt(s) * 1000);
            }
        }
        if (str.contains("m")) {
            s = str.replace("m", "");
            if (s.contains(".")) {
                s.replace(".", "");
                return (Integer.parseInt(s) * 100000);
            } else {
                return (Integer.parseInt(s) * 1000000);
            }
        }
        if (str.contains(",")) {
            s = str.replace(",", "");
            return (Integer.parseInt(s));
        }
        return Integer.parseInt(str);
    }

    /**
     * GEItem Class.
     */
    public class GEItem {

        private String name;
        private int guidePrice = 0;

        /**
         * Item data.
         * @param name          Item name.
         * @param guidePrice    Item Guide Price.
         */
        public GEItem(String name, int guidePrice) {
            this.name = name;
            this.guidePrice = guidePrice;
        }

        /**
         * Gets the item name.
         * @return Item name.
         */
        public String getName() {
            return this.name;
        }

        /**
         * Gets the Guide Price.
         * @return Item guide price.
         */
        public int getGuidePrice() {
            return this.guidePrice;
        }
    }
}
